package com.ldg.canal.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;

import com.ldg.canal.entity.ContentEntity;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

@CanalEventListener
public class MyCalnalListener {

	@Autowired
	private StringRedisTemplate redisTemplate;
	// /**
	//  * 1. 监听插入操作
	//  * @param eventType
	//  * @param rowData
	//  */
	// @InsertListenPoint
	// public void onInsertEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
	// 	System.out.println("InsertListenPoint事件触发。。。");
	// 	rowData.getAfterColumnsList().forEach(r->System.out.println(r.getName() + ":" + r.getValue()));
	// }

	// /**
	//  * 2.监听修改操作
	//  * @param eventType
	//  * @param rowData
	//  */
	// @UpdateListenPoint
	// public void onUpdateEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
	// 	System.out.println("UpdateListenPoint事件触发。。。");
	// 	rowData.getAfterColumnsList().forEach(r->System.out.println(r.getName() + ":" + r.getValue()));
	// }

	// /**
	//  * 3.监听删除操作
	//  * @param eventType
	//  * @param rowData
	//  */
	// @DeleteListenPoint
	// public void onDeleteEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
	// 	System.out.println("DeleteListenPoint事件触发。。。");
	// 	rowData.getBeforeColumnsList().forEach(r->System.out.println(r.getName() + ":" + r.getValue()));
	// }

	/**
	 * 4. 自定义事件监听
	 * @param eventType
	 * @param rowData
	 */
	@ListenPoint(table={"tb_content"},eventType = {
			CanalEntry.EventType.INSERT,
			CanalEntry.EventType.UPDATE,
			CanalEntry.EventType.DELETE,
	})
	public void onCustomEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
		String name = eventType.getValueDescriptor().getName();
		System.out.println("当前事件名称：" + name);
		// System.out.println("自定义事件监听。");
		String id = "";
		//1. 得到修改或插入操作后的数据
		ContentEntity content = new ContentEntity();
		for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
			String colName = column.getName();
			if(colName.equalsIgnoreCase("id")){
				id = column.getValue();
			}
			if(colName.equalsIgnoreCase("category_id")){
				content.setCategoryId(column.getValue());
			}
			if(colName.equalsIgnoreCase("title")){
				content.setTitle(column.getValue());
			}
			if(colName.equalsIgnoreCase("url")){
				content.setUrl(column.getValue());
			}
			if(colName.equalsIgnoreCase("pic")){
				content.setPic(column.getValue());
			}
			if(colName.equalsIgnoreCase("status")){
				content.setStatus(column.getValue());
			}
			if(colName.equalsIgnoreCase("sort_order")){
				content.setSortOrder(column.getValue());
			}
		}
		//2. 得到redis中的数据
		String contentList = redisTemplate.opsForValue().get("contentList");
		//3. 将redis中的数据转换为集合
		List<ContentEntity> contentEntities = JSON.parseArray(contentList, ContentEntity.class);
		//4. 根据事件名称，执行相关的增量操作
		switch (name){
			case "DELETE":          // 删除事件
				int index = -1;
				for (int i = 0; i < contentEntities.size(); i++) {
					if(contentEntities.get(i).getId().equalsIgnoreCase(id)){
						index = i;
						break;
					}
				}
				contentEntities.remove(index);
				break;
			case "UPDATE":          // 修改事件
				break;
			case "INSERT":          // 插入事件
				contentEntities.add(content);
				redisTemplate.opsForValue().set("contentList",JSON.toJSONString(contentEntities));
				break;
		}
	}
}
